package com.app;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

public class TeamDetailActivity extends AppCompatActivity {

    private TextView teamID;
    private TextView bisaiTV;
    private TextView shengliTV;
    private TextView wingoalTV;
    private TextView jinqiuTV;
    private TextView jifenTV;
    private TextView fenshuTV;
    private TextView detailTV;
    private TextView xinxiTV;
    private ImageView teampicIV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_detail);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        teamID = findViewById(R.id.teamid_tv);
        bisaiTV = findViewById(R.id.bisai_tv);
        wingoalTV = findViewById(R.id.wingoal_tv);
        jinqiuTV = findViewById(R.id.jinqiu_tv);
        jifenTV = findViewById(R.id.jifen_tv);
        detailTV = findViewById(R.id.detail_tv);
        teampicIV = findViewById(R.id.teampic_iv);
    }

    private void initData() {
        getData();
    }

    private void initEvent() {

    }

    private void getData() {

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://api.boruiman.com/app19/teams/findeone";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: " + jsonObject);
                /*处理结果数据*/
                // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）
                Gson gson = new Gson();
                MyBean myBean = gson.fromJson(jsonObject.toString(), MyBean.class);

                if (myBean.isSuccess()) {
                    Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();
                    // 获取数据，
                    MyBean.DataBean dataBean = myBean.getData();
                    // 在控件上显示文字
//                    mTvName.setText(dataBean.getName());
                    teamID.setText(dataBean.getTeamId());
                    bisaiTV.setText("胜:" + dataBean.getWinMatch() + "负:" + dataBean.getLooseMatch() + "平:" + dataBean.getPingMatch());
                    jinqiuTV.setText("进球:" + dataBean.getWinGoal() + "失球:" + dataBean.getLoseGoal());
                    jifenTV.setText(dataBean.getScore() + "分");
                    detailTV.setText(dataBean.getDetail());
                    // 在控件上显示图片
                    Picasso.with(getApplicationContext())
                            .load("http://api.boruiman.com/static" + dataBean.getTeamPic())
                            .into(teampicIV);
                } else {
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }


}